define(function () {
    'use strict';

    function ctrl($scope,$state,$ionicLoading,InfoService) {

        $scope.$on('$ionicView.beforeEnter', function() {
            init();
        });

        function init(){
            if(!$scope.channels){
                $ionicLoading.show();
            }

            InfoService.getChannelInfosByRoot().then(function(data){
                if(data) {
                    $scope.channels = data;
                    for(var i = 0; i < $scope.channels.length; i++) {
                        var channel = $scope.channels[i];
                        (function(c){
                            InfoService.getChannelInfosByParentId(c.id).then(
                                function(sonData) {
                                    c.son =  sonData;
                                }
                            )
                        })(channel);
                    }
                }
                $ionicLoading.hide();
            });

        }

        $scope.onBackKeyDown = function(){
            $state.go("tab.index");
        };

    }

    ctrl.$inject = ['$scope','$state','$ionicLoading','InfoService'];
    return ctrl;
});